
package com.borqs.music.tasks;

import static com.borqs.music.util.MusicConf.ReqType.API_CREATE_STREAM;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.borqs.music.executor.Task;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.http.RequestMethod;
import com.borqs.music.model.SongInfo;
import com.borqs.music.util.BpcApiUtils;
import com.borqs.music.util.MusicConf;

/**
 * Task to create a Stream post. Reference http://api.borqs.com:7027 about
 * /post/create
 * 
 * @author b431
 */
public class CreateStreamTask extends Task {

    private static final String KEY_TYPE = "type";
    private static final String KEY_MSG = "msg";
    private static final String KEY_ATTACHMENTS = "attachments";
    // private static final String KEY_PACKAGE = "package";
    // private static final String KEY_APK_ID = "apkId";
    // private static final String KEY_MENTIONS = "mentions";
    // private static final String KEY_SECRETLY = "secretly";
    private static final String KEY_APP_DATA = "appData";

    private String mTicket;
    private String mMsg;
    private String mAttachments;
    private String mAppData;

    public CreateStreamTask(String ticket, SongInfo song, String msg, String summaryFormat) {
        mTicket = ticket;

        if (msg == null) {
            mMsg = "";
        } else {
            mMsg = msg;
        }

        mAppData = prepareAppData(song);
        
        mAttachments = prepareAttachments(song, summaryFormat);
    }

    @Override
    public HttpRequest prepareRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(KEY_TYPE, String.valueOf(BpcApiUtils.MUSIC_POST));
        if (mMsg != null) {
            params.put(KEY_MSG, mMsg);
        } else {
            throw new IllegalStateException("mMsg shouldn't be null");
        }
        
        if (mAttachments != null) {
            params.put(KEY_ATTACHMENTS, mAttachments);
        } else {
            throw new IllegalStateException("mAttachments shouldn't be null");
        }

        if (mAppData != null) {
            params.put(KEY_APP_DATA, mAppData);
        }
        final String URL_CREATE_STREAM = MusicConf.getUrl(API_CREATE_STREAM);
        HttpRequest request = new HttpRequest(RequestMethod.GET, URL_CREATE_STREAM, mTicket, params);
        return request;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        return resp.asJSONObject();
    }

    private static final String JSON_ID = "id";
    private static final String JSON_SUMMARY = "summary";
    private static final String JSON_COVER_URL = "coverurl";
    private static final String JSON_NAME = "name";

    private static String prepareAttachments(SongInfo song, String summaryFormat) {
        String attachments = null;
        try {
            JSONObject jObj = new JSONObject();
            jObj.put(JSON_ID, song.id);
            String summary = String.format(summaryFormat, song.artist, song.album);
            jObj.put(JSON_SUMMARY, summary);
            jObj.put(JSON_COVER_URL, song.imgUrl);
            jObj.put(JSON_NAME, song.title);
            attachments = jObj.toString();
        } catch (JSONException jex) {
            jex.printStackTrace();
        }

        return attachments;
    }

    private static String prepareAppData(SongInfo song) {
        String appData = null;
        try {
            appData = songInfoToJson(song);
        } catch (JSONException jex) {
            jex.printStackTrace();
        }
        return appData;
    }

    private static final String JSON_TITLE = "title";
    private static final String JSON_ARTIST = "artist";
    private static final String JSON_ALBUM = "album";
    private static final String JSON_IMAGE = "imgUrl";
    private static final String JSON_FILE_NAME = "fileName";
    private static final String JSON_FILE_URL = "fileUrl";

    public static String songInfoToJson(SongInfo song) throws JSONException {
        JSONObject jObj = new JSONObject();
        jObj.put(JSON_ID, song.id);
        jObj.put(JSON_TITLE, song.title);
        jObj.put(JSON_ARTIST, song.artist);
        jObj.put(JSON_ALBUM, song.album);
        jObj.put(JSON_IMAGE, song.imgUrl);
        jObj.put(JSON_FILE_NAME, song.fileName);
        jObj.put(JSON_FILE_URL, song.fileUrl);
        return jObj.toString();
    }

    public static SongInfo jsonToSongInfo(String song) throws JSONException {
        JSONObject jObj = new JSONObject(song);
        SongInfo info = new SongInfo();
        info.id = jObj.getInt(JSON_ID);
        info.title = jObj.getString(JSON_TITLE);
        info.artist = jObj.getString(JSON_ARTIST);
        info.album = jObj.getString(JSON_ALBUM);
        info.imgUrl = jObj.getString(JSON_IMAGE);
        info.fileName = jObj.getString(JSON_FILE_NAME);
        info.fileUrl = jObj.getString(JSON_FILE_URL);
        return info;
    }
}
